<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入问卷调查名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="策略编号" prop="tacticsId">
            <el-input
              v-model="queryParams.tacticsId"
              placeholder="请输入策略编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="策略值" prop="tacticsValue">
            <el-input
              v-model="queryParams.tacticsValue"
              placeholder="请输入策略值"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="分类编号" prop="category">
            <el-input
              v-model="queryParams.category"
              placeholder="请输入分类编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入标题"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="唯一标识" prop="unCode">
            <el-input
              v-model="queryParams.unCode"
              placeholder="请输入唯一标识"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="排序号" prop="sortNo">
            <el-input
              v-model="queryParams.sortNo"
              placeholder="请输入排序号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="启用状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择启用状态 0-启用，1-暂停" clearable size="small">
              <el-option label="请选择字典生成" value="" v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value"
              />

            </el-select>
          </el-form-item>
          <el-form-item label="父id" prop="parentId">
            <el-input
              v-model="queryParams.parentId"
              placeholder="请输入父id"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="跳转地址" prop="url">
            <el-input
              v-model="queryParams.url"
              placeholder="请输入初始化页面时判断用户是否完成了该问卷，完成了之后的跳转地址"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="场景总分" prop="questionValue">
            <el-input
              v-model="queryParams.questionValue"
              placeholder="请输入场景总分(试卷总分)"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:scenario:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:scenario:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:scenario:remove']"
            >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:scenario:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="scenarioList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="策略编号" align="center" prop="tacticsId"/>
      <el-table-column label="策略类型" align="center" prop="tacticsType"/>
      <el-table-column label="策略值" align="center" prop="tacticsValue"/>
      <el-table-column label="分类编号" align="center" prop="category"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="唯一标识" align="center" prop="unCode"/>
      <el-table-column label="排序号" align="center" prop="sortNo"/>
      <el-table-column label="启用状态" align="center" prop="status"/>
      <el-table-column label="父id" align="center" prop="parentId"/>
      <el-table-column label="跳转地址" align="center" prop="url"/>
      <el-table-column label="场景总分" align="center" prop="questionValue"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:scenario:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:scenario:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务场景管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="策略类型" prop="tacticsId">
          <!--          <el-input v-model="form.tacticsId" placeholder="请输入策略编号"/>-->
          <el-select v-model="form.tacticsId" clearable style="width: 100%;" placeholder="请选择策略类型">
            <el-option
              v-for="item in tacticsList"
              :key="item.tacticsId"
              :label="item.tacticsId+' - '+ item.tacticsType"
              :value="item.tacticsId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="策略值" prop="tacticsValue" v-if="form.tacticsId != 1">
          <el-input v-model="form.tacticsValue" placeholder="请输入策略值"/>
        </el-form-item>
        <!--        <el-form-item label="场景编号" prop="category">-->
        <!--          <el-input v-model="form.category" placeholder="请输入分类编号"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="场景标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <!--        <el-form-item label="唯一标识" prop="unCode">-->
        <!--          <el-input v-model="form.unCode" placeholder="请输入唯一标识"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="排序号" prop="sortNo">
          <el-input v-model="form.sortNo" placeholder="请输入排序号"/>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">暂停</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父id" prop="id">
          <!--          <el-input v-model="form.parentId" placeholder="请输入父id"/>-->
          <treeselect v-model="form.parentId" :options="categoryOptions" :normalizer="normalizer" :show-count="true" placeholder="请选择父类机构" />
        </el-form-item>
        <el-form-item label="跳转地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入初始化页面时判断用户是否完成了该问卷，完成了之后的跳转地址"/>
        </el-form-item>
        <el-form-item label="场景总分" prop="questionValue">
          <el-input v-model="form.questionValue" placeholder="请输入场景总分(试卷总分)"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listScenario,
  getScenario,
  delScenario,
  addScenario,
  updateScenario,
  exportScenario,
  treeselect
} from '@/api/question/scenario'
import { listTactics } from '@/api/question/tactics'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: 'Scenario',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 问卷调查名称
      categoryName: undefined,
      // 问卷调查树选项
      categoryOptions: undefined,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务场景管理表格数据
      scenarioList: [],
      // 业务场景策略管理表格数据
      tacticsList: [],
      defaultProps: {
        children: 'childList',
        label: 'title'
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id: null,
        pageNum: 1,
        pageSize: 10,
        tacticsId: null,
        tacticsType: null,
        tacticsValue: null,
        category: null,
        title: null,
        unCode: null,
        sortNo: null,
        status: null,
        parentId: null,
        url: null,
        questionValue: null
      },
      options: [{
        value: '0',
        label: '0-启用'
      }, {
        value: '1',
        label: '1-暂停'
      }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tacticsId: [
          { required: true, message: '请选择策略编号', trigger: 'blur' }
        ],
        parentId: [
          { required: true, message: '父id不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getTreeselect()

  },
  watch: {
    // 根据名称筛选部门树
    categoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    /** 查询业务场景策略管理列表 */
    getTacticsList() {
      listTactics(this.queryParams).then(response => {
        this.tacticsList = response.rows
        console.log(this.tacticsList)
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.title.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      if(data.id != 1){
        this.queryParams.id = data.id
      }else{
        this.queryParams.id= null
      }
      this.getList()
    },
    /** 查询业务场景下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.categoryOptions = response.data
        console.log(this.categoryOptions)
      })
    },
    /** 查询业务场景管理列表 */
    getList() {
      this.loading = true
      listScenario(this.queryParams).then(response => {
        this.scenarioList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.childList
      };
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tacticsId: null,
        tacticsValue: null,
        category: null,
        title: null,
        unCode: null,
        sortNo: null,
        status: '0',
        parentId: null,
        url: null,
        questionValue: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.getTreeselect()

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      // 查询问卷场景策略分类列表
      this.getTacticsList()
      // 场景父类列表
      // this.getTreeselect()
      this.open = true
      this.title = '添加业务场景管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // 查询问卷场景策略分类列表
      this.getTacticsList()
      // 场景父类列表
      this.getTreeselect()
      const id = row.id || this.ids
      getScenario(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改业务场景管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateScenario(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
              this.getTreeselect()

            })
          } else {
            addScenario(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
              this.getTreeselect()

            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除业务场景管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delScenario(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有业务场景管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportScenario(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
